<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\goods\model\CodeRule as CodeRuleModel;

/**
 * 编码规则
 */
class Coderule extends Admin
{
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $order = $this->getOrder('codeid DESC');
        $map = [];
        $map = $this->getMap();
        $data_list = CodeRuleModel::codeselect($map,$order);       
        return ZBuilder::make('table')
        ->setSearch(['codename' => '名称'])
        ->addColumns([ // 批量添加列
            ['codename', '名称'],
            ['rule', '规则'],
            ['status', '状态' , 'status'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_coderule')
        ->setPrimaryKey('codeid')
        ->addRightButton('edit')
        ->addRightButton('delete')
        ->addTopButton('add')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $res = $this->validate($data, 'CodeRule');
            if(true !== $res) return $this->error($res);
            if($data['codevalue2'] == $data['codevalue4'] || $data['codevalue2'] == $data['codevalue6'] || $data['codevalue4'] == $data['codevalue6']){
                $this->error('款类、颜色、尺码不能重复');
            }
            $result = CodeRuleModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        $data_list = ['1' => '款自编码', '4' => '款货号', '2' => '颜色自编码', '5' => '颜色名称', '3' =>' 尺码自编码', '6' => '尺码名称'];


        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'codename', '名称'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],  
            ['text', 'codevalue1', '自定义值',''],
            ['select', 'codevalue2', '选值', '必填', $data_list],
            ['text', 'codevalue3', '自定义值',''],
            ['select', 'codevalue4', '选值', '必填', $data_list],
            ['text', 'codevalue5', '自定义值',''],
            ['select', 'codevalue6', '选值', '必填', $data_list],
            ['text', 'codevalue7', '自定义值',''],          
        ])
        ->layout(['codename' => 6, 'status' => 6, 'codevalue1' => 3, 'codevalue2' => 3, 'codevalue3' => 3, 'codevalue4' => 3, 'codevalue5' => 3, 'codevalue6' => 3, 'codevalue7' => 3])
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $res = $this->validate($data, 'CodeRule');
            if(true !== $res) return $this->error($res);
            if($data['codevalue2'] == $data['codevalue4'] || $data['codevalue2'] == $data['codevalue6'] || $data['codevalue4'] == $data['codevalue6']){
                $this->error('款类、颜色、尺码不能重复');
            }
            $result = CodeRuleModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        $info = CodeRuleModel::where('codeid',$id)->find();

        $data_list = ['1' => '款自编码', '4' => '款货号', '2' => '颜色自编码', '5' => '颜色名称', '3' =>' 尺码自编码', '6' => '尺码名称'];


        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'codeid'],            
            ['text', 'codename', '名称'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],  
            ['text', 'codevalue1', '自定义值',''],
            ['select', 'codevalue2', '选值', '必填', $data_list],
            ['text', 'codevalue3', '自定义值',''],
            ['select', 'codevalue4', '选值', '必填', $data_list],
            ['text', 'codevalue5', '自定义值',''],
            ['select', 'codevalue6', '选值', '必填', $data_list],
            ['text', 'codevalue7', '自定义值',''],          
        ])
        ->setFormData($info)
        ->layout(['codename' => 6, 'status' => 6, 'codevalue1' => 3, 'codevalue2' => 3, 'codevalue3' => 3, 'codevalue4' => 3, 'codevalue5' => 3, 'codevalue6' => 3, 'codevalue7' => 3])
        ->fetch();
    }


    /**
    *删除
    */
    public function delete($ids = 0){
        $id   = input('ids');
        $result = CodeRuleModel::where(array('codeid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }


   

    
   
}
